#include "capilot_data_receiver.h"

#include "adr_component.h"

CapilotDataReceiver::CapilotDataReceiver()
{
}

CapilotDataReceiver::~CapilotDataReceiver()
{
}

// void CapilotDataReceiver::init(std::shared_ptr<ca::basic::cm::AsyncSubscriber> sub, const std::string &topic, std::function<int(const std::string &topic,const ca::basic::core::RawBuffer *data, const ca::basic::core::RawBuffer *header)> func)
// {
//     m_callBack = func;
//     m_topic = topic;
//     sub->subscribe(topic, std::bind(&CapilotDataReceiver::onObjectCallback, this, std::placeholders::_1, std::placeholders::_2,std::placeholders::_3));
// }
void CapilotDataReceiver::init(adr_component *handle, const std::string &topic, std::function<void(const std::string &, const capilot::RawMessage &, const double &)> func)
//void CapilotDataReceiver::init(adr_component *handle, const std::string &topic, std::function<void(const std::string &, const std::string &, const double &)> func)
{
    m_callBack = func;
    m_topic = topic;
    handle->bindEventCallback<capilot::RawMessage>(std::bind(&CapilotDataReceiver::onObjectCallback, this, std::placeholders::_1, std::placeholders::_2), topic); //新增
}

void CapilotDataReceiver::initRpc(adr_component *handle, const std::string &topic, std::function<void(const std::string &, const std::string &, std::string *)> func)
{
    m_callBack_Rpc = func;
    m_topic_rpc = topic;

    std::function<void(const std::string&, std::string*)> rpc_func = std::bind(&CapilotDataReceiver::onObjectRpcCallback, this, std::placeholders::_1, std::placeholders::_2);
    handle->BindRequest<std::string, std::string>(rpc_func, m_topic_rpc);
}

// int CapilotDataReceiver::onObjectCallback(const std::string &topic, const ca::basic::core::RawBuffer *data, const ca::basic::core::RawBuffer *header)
// {
//     m_callBack(topic,data,header);
//     return 0;
// }
void CapilotDataReceiver::onObjectCallback(const capilot::RawMessage &data, double time)
{
    m_callBack(m_topic, data, time);
}

void CapilotDataReceiver::onObjectRpcCallback(const std::string &requestData, std::string *replyData)
{
    m_callBack_Rpc(m_topic_rpc, requestData, replyData);
}